﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToRTR.CApExportToRTRExporter
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExportToRTR.YAML;
using EngineeringInternalExtension;
using System;
using System.IO;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToRTR{

public abstract class CApExportToRTRExporter
{
  internal const float SCALE = 0.001f;
  internal const string MESHES_DIR = "meshes";
  protected string m_root;
  protected string m_parentLink;
  protected string m_directory;

  public CApExportToRTRExporter(string root, string parentLink, string directoryName)
  {
    this.m_root = root;
    this.m_parentLink = parentLink;
    this.m_directory = $"meshes{Path.AltDirectorySeparatorChar.ToString()}{CApExportToRTRUtils.GetValidFileName(directoryName)}";
  }

  protected CApExportToRTRVisual CreateVisual(
    string stlFileName,
    TxTransformation originTransformation = null)
  {
    return new CApExportToRTRVisual(new CApExportToRTRGeometry(Path.Combine(this.m_directory + "/", stlFileName)), origin: TxTransformation.op_Inequality(originTransformation, (TxTransformation) null) ? this.CreateOrigin(originTransformation) : (CApExportToRTROrigin) null);
  }

  protected void CreateCadFile(ITxObject obj, string cadFileName)
  {
    new CApExportToRTRSTLWriter(obj).Save(Path.Combine(this.m_root, this.m_directory, cadFileName));
  }

  protected CApExportToRTROrigin CreateOrigin(TxTransformation location)
  {
    TxVector txVector = TxVector.op_Multiply(location.Translation, 1.0 / 1000.0);
    TxVector rotationRpyXyz = location.RotationRPY_XYZ;
    return new CApExportToRTROrigin(new double[3]
    {
      txVector.X,
      txVector.Y,
      txVector.Z
    }, new double[3]
    {
      rotationRpyXyz.X,
      rotationRpyXyz.Y,
      rotationRpyXyz.Z
    });
  }

  protected string Export(ITxLocatableObject obj)
  {
    string cadFileName = this.ExportCad(obj);
    ObjectInstance yamlObject = this.ExportObject(obj, cadFileName);
    this.CreateMate(obj, yamlObject);
    return cadFileName;
  }

  private string CalcPrototype(ITxLocatableObject obj)
  {
    string str = string.Empty;
    if (obj is ITxComponent)
      str = !CApExportToRTRUtils.IsVisibleAndHaveApprox(obj as ITxDisplayableObject) ? "emptyLink" : TxComponentEx.GetComponentPath(obj as ITxComponent, false);
    else if (obj is TxKinematicLink)
      str = !CApExportToRTRUtils.IsEmptyLink(obj as TxKinematicLink) ? $"{TxComponentEx.GetComponentPath(((ITxObject) obj).Collection as ITxComponent, false)}_{((ITxObject) obj).Name}" : "emptyLink";
    else if (obj is ITx2Or3DimensionalGeometry)
      str = $"{TxComponentEx.GetComponentPath(TxEngineeringDataInternal.GetScope((ITxObject) obj) as ITxComponent, false)}_{((ITxObject) ((ITxObject) obj).Collection).Name}_{((ITxObject) obj).Name}";
    if (obj is ITxDevice && str != "emptyLink")
    {
      using (TxPoseData currentPose = (obj as ITxDevice).CurrentPose)
        str = $"{str}_{string.Join(",", currentPose.JointValues.ToArray())}";
    }
    return str;
  }

  private void HandleEmptyLink()
  {
    if (CApExportToRTRSTLWriter.Prototypes.ContainsKey("emptyLink"))
      return;
    string path3 = $"{"emptyLink"}{".jt"}";
    File.Copy(Path.Combine(TxApplication.InstallationDirectory, "NewWorld\\Data\\defaultPlp.co\\defaultPlp.jt"), Path.Combine(this.m_root, this.m_directory, path3));
    CApExportToRTRSTLWriter.Prototypes.Add("emptyLink", path3);
  }

  protected virtual string ExportCad(ITxLocatableObject obj, ITxLocatableObject descendant = null)
  {
    string key = descendant != null ? this.CalcPrototype(descendant) : this.CalcPrototype(obj);
    string cadFileName;
    if (key == "emptyLink")
      cadFileName = key;
    else if (!string.IsNullOrEmpty(key) && CApExportToRTRSTLWriter.Prototypes.ContainsKey(key))
    {
      cadFileName = CApExportToRTRSTLWriter.Prototypes[key];
    }
    else
    {
      cadFileName = descendant != null ? CApExportToRTRSTLWriter.GetStlFileName((ITxObject) descendant, suffix: ".jt") : CApExportToRTRSTLWriter.GetStlFileName((ITxObject) obj, suffix: ".jt");
      this.CreateCadFile((ITxObject) obj, cadFileName);
      if (!string.IsNullOrEmpty(key))
        CApExportToRTRSTLWriter.Prototypes.Add(key, cadFileName);
    }
    return cadFileName;
  }

  protected virtual ObjectInstance ExportObject(ITxLocatableObject obj, string cadFileName)
  {
    ObjectInstance yamlObj = new ObjectInstance()
    {
      Name = TxObjectEx.GetUniqueNameForRTR((ITxObject) obj, false),
      CadPath = cadFileName == "emptyLink" ? (string) null : Path.Combine(this.m_directory + "/", cadFileName)
    };
    CApExportToRTRYamlMgr.Instance.AddObj<ObjectInstance>(yamlObj, (ITxObject) obj);
    return yamlObj;
  }

  protected virtual void CreateMate(ITxLocatableObject obj, ObjectInstance yamlObject)
  {
    throw new NotImplementedException();
  }
}
